package com.hod.client.room;

import com.google.gwt.core.client.GWT;
import com.google.gwt.gears.client.database.DatabaseException;
import com.google.gwt.user.client.Window;
import com.hod.client.backend.orm.client.GearsDataStore;
import com.hod.client.backend.orm.client.GearsQuery;
import com.hod.client.roomtype.RoomType;
import com.hod.client.roomtype.RoomTypeCRUD;
import com.hod.client.roomtype.RoomTypeRecordset;

public class RoomRecordset {
	
	/*
     * Busco una habitacion en particular sabiendo el Id
     */
 	public static Room getRoomById(Integer idRoom) {
 		// TODO Auto-generated method stub
 		try {
 			GearsDataStore dsRoom = (GearsDataStore) GWT.create(Room.class);
 			Room o = (Room) dsRoom.findById(idRoom);
 			return o;
 		} catch (DatabaseException e) {
 			// TODO Auto-generated catch block
 			return null;
 		} 
 	}
	
    /*
     * Listo las habitaciones disponibles
     */
 	public static Room[] getRoomsAvailable() {
 		// TODO Auto-generated method stub
 		try {
 			GearsDataStore dsRoom = (GearsDataStore) GWT.create(Room.class);
 			GearsQuery q = dsRoom.getNamedQuery(Room.FIND_AVAILABLE);
 			q.setString(0, "FREE");
 			Room[] data = (Room[]) q.execute();
 			return data;
 		} catch (DatabaseException e) {
 			// TODO Auto-generated catch block
 			return null;
 		} 
 	}
 	
 	public static Room[] getFreeRoomsByType(String idRoomType) {
 		// TODO Auto-generated method stub
 		try {
 			/*
 			 * Las habitaciones del tipo solicitado + que esten libres
 			 */
 			GearsDataStore dsRoom = (GearsDataStore) GWT.create(Room.class);
 			GearsQuery q = dsRoom.getNamedQuery(Room.FIND_BY_TYPE);
 			q.setString(0, idRoomType);
 			q.setString(1, "FREE");
 			Room[] data = (Room[]) q.execute();
 			return data;
 		} catch (DatabaseException e) {
 			// TODO Auto-generated catch block
 			return null;
 		} 
 	}
    /**
     * Cambio el estado de la habitacion.
     * Si el caso de FREE => libera una habitacion para el tipo asociado a la habitacion
     * Si el caso de BUSY => quita una habitacion para el tipo asociado a la habitacion
     * @param idRoom
     * @param state
     * @return
     */
	
	public static boolean setRoomState(String idRoom,String state){
		GearsDataStore dsRoom = (GearsDataStore) GWT.create(Room.class);
		try {
			Room o = (Room) dsRoom.findById(new Integer(idRoom));
			o.setState(state);
			if (state.compareTo("FREE")==0){
				RoomTypeRecordset.increaseRoomTypeCapacity(o.getIdRoomType());	
			}
			if (state.compareTo("BUSY")==0){
				RoomTypeRecordset.decreaseRoomTypeCapacity(o.getIdRoomType());
			} 
			dsRoom.save(o);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			Window.alert(e.getMessage());
			return false;
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			Window.alert(e.getMessage());
			return false;
		}
		return true;
	}
	
	public static Room[] getRooms() {
		// TODO Auto-generated method stub
		try {
 			GearsDataStore dsRoom = (GearsDataStore) GWT.create(Room.class);
 			GearsQuery q = dsRoom.getNamedQuery(Room.FIND_ALL);
 			Room[] data = (Room[]) q.execute();
 			return data;
 		} catch (DatabaseException e) {
 			// TODO Auto-generated catch block
 			return null;
 		} 
	}

	public static double calculateRoomCharge(Integer idRoom) {
		// TODO Auto-generated method stub
		Window.alert("Entre a la habitacion: "+idRoom);
		GearsDataStore ds = (GearsDataStore) GWT.create(Room.class);
		Room o;
		try {
			o = (Room) ds.findById(idRoom);
			Window.alert("Existe la habitacion");
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			Window.alert("Error");
			return 0;
		}
		RoomType rt = RoomTypeRecordset.getRoomType(o.getIdRoomType());		
		if (rt!=null) return rt.getPrice();
		return 0;
	}
	
	public static Room[] getRoomsByFloor() {
		// TODO Auto-generated method stub
		try {
 			GearsDataStore dsRoom = (GearsDataStore) GWT.create(Room.class);
 			GearsQuery q = dsRoom.getNamedQuery(Room.FIND_BY_FLOOR);
 			Room[] data = (Room[]) q.execute();
 			return data;
 		} catch (DatabaseException e) {
 			// TODO Auto-generated catch block
 			return null;
 		} 
	}
	

}
